package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.entity.*;
import org.jeecg.modules.monitoring.j_monitoring.mapper.EquipmentDataMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MCabinetMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlNetworkResourceMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MMachineRoomMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlNetworkResourceService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 网络资源(管理)
 * @Author: xiaoyq
 * @Date:   2023-01-11
 * @Version: V1.0
 */
@Service
public class MGlNetworkResourceServiceImpl extends ServiceImpl<MGlNetworkResourceMapper, MGlNetworkResource> implements IMGlNetworkResourceService {

    @Resource
    private EquipmentDataMapper equipmentDataMapper;
    @Resource
    private MCabinetMapper cabinetMapper;
    @Resource
    private MCabinetServiceImpl cabinetService;
    @Resource
    private MMachineRoomServiceImpl roomService;
    @Resource
    private MMachineRoomMapper mMachineRoomMapper;

    @Override
    public void insertByEquipment(List<String> ids){
        MGlNetworkResource mGlNetworkResource = new MGlNetworkResource();
        QueryWrapper<MGlNetworkResource> queryWrapper = QueryGenerator.initQueryWrapper(mGlNetworkResource, new HashMap<>());
//        queryWrapper.eq("del_flag",'0');
        List<MGlNetworkResource> networkResourceList = baseMapper.selectList(queryWrapper);

        List<EquipmentData> dataList = equipmentDataMapper.selectBatchIds(ids);
        for (EquipmentData data : dataList){
            if (this.canInsert(networkResourceList,data.getCode())){
                //序列号重复不可添加
                MGlNetworkResource resource = new MGlNetworkResource();
                resource.setName(data.getDevicestypename());
                resource.setModel(data.getModel());
                resource.setEquipmentSerialNumber(data.getCode());
                resource.setNumberOfPorts(new Integer(data.getUsage()));
                baseMapper.insert(resource);
            }
        }
    }

    private Boolean canInsert(List<MGlNetworkResource> list,String code){
        for (MGlNetworkResource resource : list){
            if (resource.getEquipmentClassification().equals(code)){
                return false;
            }
        }
        return true;
    }

    @Override
    public void test(){
        MGlNetworkResource mGlNetworkResource = new MGlNetworkResource();
        QueryWrapper<MGlNetworkResource> queryWrapper = QueryGenerator.initQueryWrapper(mGlNetworkResource, new HashMap<>());
//        queryWrapper.eq("del_flag",'0');
        List<MGlNetworkResource> networkResourceList = baseMapper.selectList(queryWrapper);
        for (MGlNetworkResource data : networkResourceList){
            if (StringUtil.isEmpty(data.getCabinetId())){
                List<MCabinet> cabinets = cabinetMapper.selectByName(data.getCabinet());
                if (cabinets.size()>0){
                    data.setCabinetId(cabinets.get(0).getId());
                    baseMapper.updateById(data);
                }
            }
        }



    }



    public boolean saveBatch(Collection<MGlNetworkResource> entityList, int size){

        //机柜列表
        List<MCabinet> cabinetList = cabinetMapper.selectAll();

        //机房列表
        List<MMachineRoom> roomList = mMachineRoomMapper.selectAll();

        //处理机房机柜id绑定问题
        for (MGlNetworkResource entity : entityList){
            entity.setCabinetId(cabinetService.getId(entity.getCabinet(),cabinetList));
            entity.setMachineRoomId(roomService.getId(entity.getMachineRoom(),roomList));
        }


        String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
        return this.executeBatch(entityList, size, (sqlSession, entity) -> {
            sqlSession.insert(sqlStatement, entity);
        });

    }


    public String getIp(String id){
        return baseMapper.selectById(id).getManageIp();
    }
}
